package sdk.webview.fmc.com.fmcsdk.dialog;

import android.annotation.SuppressLint;
import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;

import sdk.webview.fmc.com.fmcsdk.R;

public class CenterTipDialog extends Dialog {
    private Context context;
    private String msg;
    private String cancelTip;
    private boolean isShowCanel = true;
    private OnHandlerListener listener;

    public CenterTipDialog(@NonNull Context context, String msg, String cancelTip, OnHandlerListener listener) {
        super(context, R.style.Dialog);
        this.context = context;
        this.msg = msg;
        this.cancelTip = cancelTip;
        this.listener = listener;
    }

    public CenterTipDialog(@NonNull Context context, String msg, String cancelTip, boolean isShowCanel, OnHandlerListener listener) {
        super(context, R.style.Dialog);
        this.context = context;
        this.msg = msg;
        this.cancelTip = cancelTip;
        this.listener = listener;
        this.isShowCanel = isShowCanel;
    }

    @SuppressLint("SetTextI18n")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.dialog_tip);

        ImageView ivClose = findViewById(R.id.iv_close);
        TextView tvCancel = findViewById(R.id.tv_cancel);
        TextView tvMsg = findViewById(R.id.tv_message_content);
        TextView tvHandler = findViewById(R.id.tv_handle);

        if (!isShowCanel) {
            tvCancel.setVisibility(View.GONE);
        } else {
            tvCancel.setVisibility(View.VISIBLE);
        }

        tvMsg.setText(msg);
        ivClose.setOnClickListener(v -> dismiss());

        if (null != cancelTip) {
            tvCancel.setText(cancelTip);
        }
        tvCancel.setOnClickListener(v -> {
            if (null != listener) {
                listener.onNotHandlerClick();
            }
            dismiss();
        });

        tvHandler.setOnClickListener(v -> {
            if (null != listener) {
                listener.onHandlerClick();
            }
            dismiss();
        });


        //试着提示框的位置和大小
        Window dialogWindow = getWindow();
        WindowManager.LayoutParams lp = dialogWindow.getAttributes();
        DisplayMetrics d = context.getResources().getDisplayMetrics(); // 获取屏幕宽、高
        lp.width = (int) (d.widthPixels * 0.9);
        dialogWindow.setAttributes(lp);
    }


    public interface OnHandlerListener {
        void onHandlerClick();

        void onNotHandlerClick();
    }

}
